/** 
 * Forward declaration of guess API.
 * @param  num   your guess
 * @return 	     -1 if num is higher than the picked number
 *			      1 if num is lower than the picked number
 *               otherwise return 0
 * int guess(int num);
 */

public class Solution extends GuessGame {
    public int guessNumber(int n) {
        int p=1;
        int q=n;
        while(q>=p)
        {
            int mid=p+(q-p)/2;
            if(guess(mid)==-1)
            {
                q=mid-1;
            }
            else if(guess(mid)==1)
            {
                p=mid+1;
            }
            else
            {
                return mid;
            }
        }
        return -1;
    }
}
